﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WLFramework.Validation;
using WLFramework.Json;
using WLFramework.Web;
using WLFramework.DataAccess;
using GenericHtml;

namespace SampleSite.admin.ajax
{
    /// <summary>
    /// updatePosition 的摘要说明
    /// </summary>
    public class updatePosition : AjaxHttpHandler
    {
        private class InputData
        {
            [Required]
            public int P_ID { get; set; }

            [Required]
            public int P_Org { get; set; }

            [Required]
            public int P_Level { get; set; }

            [Required]
            public string P_CName { get; set; }

            public string P_EName { get; set; }

        }

        public override void OnProcessRequest(HttpContext context)
        {
            InputData input;
            var errors = context.GetModel<InputData>(out input);
            if (errors.Count == 0)
            {
                using (PermissionContext db = new PermissionContext())
                {

                    var accessor = DataAccessor.CreateInstance<PermissionCommand>(db);
                    db.BeginTransaction();

                    var result = accessor.UpdatePosition(
                        input.P_ID,
                        input.P_Org,
                        input.P_CName,
                        input.P_EName,
                        input.P_Level);

                    if (result.ErrorID != 0)
                        throw new Exception(result.ErrorMsg);

                    db.CommitTransaction();
                }

                context.Response.Write(JSON.Instance.ToJSON(JSONResult.Success));
            }
            else
            {
                throw new Exception("输入的数据格式不正确");
            }
        }
    }
}